package com.testmanager.testmananger.shuiyou.strCompare.respository;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.testmanager.testmananger.shuiyou.strCompare.domain.QSySqlCompareScriptEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SySqlCompareScriptEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.SqlCompareScriptSearchDto;
import com.testmanager.testmananger.shuiyou.strCompare.service.SyProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Slf4j
@Repository
@Transactional(readOnly = true)
public class SySqlCompareScriptCustom {

    @Autowired
    SySqlCompareRepository sySqlCompareRepository;

    @Autowired
    SyProjectService syProjectService;

    public Page<SySqlCompareScriptEntity>  search(SqlCompareScriptSearchDto sqlCompareScriptSearchDto){


        log.info("SyCompareScriptCustom:{}",sqlCompareScriptSearchDto);

        QSySqlCompareScriptEntity qSySqlCompareScriptEntity = QSySqlCompareScriptEntity.sySqlCompareScriptEntity;

        BooleanExpression expression = qSySqlCompareScriptEntity.id.isNotNull();

        expression = expression.and(qSySqlCompareScriptEntity.status.eq(1));

        if (sqlCompareScriptSearchDto.getProjectId() != 0){
            //expression = expression.and(qSySqlCompareScriptEntity.projectid.eq(sqlCompareScriptSearchDto.getProjectId()));
            expression = expression.and(qSySqlCompareScriptEntity.projectid.in(syProjectService.findAllChildRenIds(sqlCompareScriptSearchDto.getProjectId())));
        }

        if (null != sqlCompareScriptSearchDto.getScriptName() && "" != sqlCompareScriptSearchDto.getScriptName() && !StringUtils.isEmpty(sqlCompareScriptSearchDto.getScriptName())){

            expression = expression.and(qSySqlCompareScriptEntity.name.like("%"+sqlCompareScriptSearchDto.getScriptName()+"%"));
        }
        if (null != sqlCompareScriptSearchDto.getCreateBy() && "" != sqlCompareScriptSearchDto.getCreateBy() && !StringUtils.isEmpty(sqlCompareScriptSearchDto.getCreateBy())){

            expression = expression.and(qSySqlCompareScriptEntity.createBy.like("%"+sqlCompareScriptSearchDto.getCreateBy()+"%"));
        }
        if (null != sqlCompareScriptSearchDto.getMouduleName() && "" != sqlCompareScriptSearchDto.getMouduleName() && !StringUtils.isEmpty(sqlCompareScriptSearchDto.getMouduleName())){

            expression = expression.and(qSySqlCompareScriptEntity.mouduleName.like("%"+sqlCompareScriptSearchDto.getMouduleName()+"%"));
        }

        Sort order = Sort.by(Sort.Order.desc(qSySqlCompareScriptEntity.createdDate.getMetadata().getName()),
                Sort.Order.desc(qSySqlCompareScriptEntity.projectid.getMetadata().getName()));

        Pageable pageable = PageRequest.of(sqlCompareScriptSearchDto.getPageNum()-1,sqlCompareScriptSearchDto.getPageSize(),order);

        return sySqlCompareRepository.findAll(expression,pageable);

       // return null;

    }


}
